<?php

class Uuch_Reader_AmazonReader extends Uuch_Reader_ProductReader{
    
    public function getInfo($url) {
        $client     = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response   = $client->request();
        $body       = $response->getBody();
        
        $uri        = parse_url($url);
        $name       = explode('.',$uri['host']);
        $reader     = ucfirst(strtolower($name[count($name)-2]));
        $config     = Application_Model_ReaderConfig::getBySite($reader);
        
        $product    = new Application_Model_Product();
        $matches    = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $matches[0] = strip_tags($matches[0]);
            $encode = mb_detect_encoding($matches[0], array('ASCII', 'GB2312', 'GBK', 'UTF-8'));
            if($encode == 'ASCII'){
                $matches[0] = mb_convert_encoding($matches[0], 'UTF-8', 'HTML-ENTITIES');
            }
            $product->Name  = $matches[0];
        }
        
        if(preg_match($config->PriceRegex, $body,$matches)){
            $matches[0]     = "<span id=" . $matches[0] . "</span>";
            $matches        = strip_tags($matches[0]);
            $matches        = str_replace("￥", "", $matches);
            $matches        = str_replace(",", "", $matches);
            $product->Price = $matches;
        }

        if($product->Price > 29){
            $product->ShipFee = 0;
        }else{
            $product->ShipFee = 5;
        }

        if(preg_match($config->ImageRegex, $body,$matches)){
            if(preg_match('/http.+?"/', $matches[0],$matches)){
                $product->RemoteImage = $product->Image = substr($matches[0], 0, strlen($matches[0])-1);
            }
        }
        
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id = $matches[2];
        }
        
        $product->Sku       = $id;
        $product->Url       = $url;

        $product->Quantity  = 999;
        $shopName           = '亚马逊';
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (null == $shop){
            $shop   = new Application_Model_Shop();
            $shop->Name     = $shopName;
            $shop->IsTaobao = true;
            $shop->Url      = $uri['host'];
            $shop->save();
        }
        $product->ShopId    = $shop->Id;
        $product->save();
        return $product;
    }
    
    public function getInfoForNew($url) {
        $client     = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response   = $client->request();
        $body       = $response->getBody();
        
        $uri        = parse_url($url);
        $name       = explode('.',$uri['host']);
        $reader     = ucfirst(strtolower($name[count($name)-2]));
        $config     = Application_Model_ReaderConfig::getBySite($reader);
        
        $product    = new Application_Model_Product();
        $matches    = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $matches[0] = strip_tags($matches[0]);
            $encode = mb_detect_encoding($matches[0], array('ASCII', 'GB2312', 'GBK', 'UTF-8'));
            if($encode == 'ASCII'){
                $matches[0] = mb_convert_encoding($matches[0], 'UTF-8', 'HTML-ENTITIES');
            }
            $product->Name  = $matches[0];
        }
        
        if(preg_match($config->PriceRegex, $body,$matches)){
            $matches[0]     = "<span id=" . $matches[0] . "</span>";
            $matches        = strip_tags($matches[0]);
            $matches        = str_replace("￥", "", $matches);
            $matches        = str_replace(",", "", $matches);
            $product->Price = $matches;
        }

        if (!$product->Price) {
            return NULL;
        }
        if($product->Price > 29){
            $product->ShipFee = 0;
        }else{
            $product->ShipFee = 5;
        }

        if(preg_match($config->ImageRegex, $body,$matches)){
            if(preg_match('/http.+?"/', $matches[0],$matches)){
                $product->RemoteImage = $product->Image = substr($matches[0], 0, strlen($matches[0])-1);
            }
        }
        
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id = $matches[2];
        }
        
        $product->Sku       = $id;
        $product->Url       = $url;

        $product->Quantity  = 999;
        $shopName           = '亚马逊';
 
        return array(
            'product'   => $product, 
            'shopName'  => $shopName,
            'siteName'  => '亚马逊',
            'shopUrl'   => $url,
            'url'       => $url,
            'siteUrl'   => $uri['scheme'] . '://' . $uri['host']);
    }
    
}